package com.appcenter.application.mapper;

import com.appcenter.application.input.User.*;
import com.appcenter.application.model.Users;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;
import java.util.List;

@Repository
@org.apache.ibatis.annotations.Mapper
public interface UsersMapper extends Mapper<Users> {

    @Select("select * from users where usercode=#{UserCode}")
    Users getUsersByUserCode(String UserCode);

    @Select("select * from users where usercode=#{UserCode} and Id <> #{Id}")
    Users getUsersByUserCodeNoId(@Param("UserCode") String UserCode, @Param("Id") String Id);


    @Select("select * from users where id = #{Id}")
    Users getUserById(String Id);

    @Select("select * from users where UserCode = #{UserCode}")
    Users getUserByUserCode(String UserCode);

    @Results({
            @Result(column = "PID", property = "UserAddress", one = @One(select = "com.appcenter.application.mapper.UserAddrsMapper.getUserAddsByUserId"))
    })
    @Select("<script>" +
            "select *,Id as PID from users where 1=1 " +
            "" +
            " <when test='keyword!=null'>" +
            " AND (UserCode like concat('%',#{keyword},'%') or UserName like concat('%',#{keyword},'%') or DingUserId like concat('%',#{keyword},'%'))" +
            " </when>" +
            "" +
            "</script>")
    List<Users> searchUsers(GetDataUsersInput input);

    @Select("select * from  users where UserCode=#{UserCode} or Id=#{Id}")
    Users getUser(GetUserInput input);

    @Select("<script>" +
            " select Users.* from Users INNER JOIN  OrgUsers ON Users.Id=OrgUsers.UserId"+"" +
            " INNER JOIN Orgs on  OrgUsers.OrgId=Orgs.Id"+
            ""+
            " <when test='IsOneLevel'>" +
            " where Orgs.OrgCode=#{OrgCode} "+
            " </when>" +
            ""+
            " <when test='!IsOneLevel'>" +
            "  where OrgUsers.OrgCodePath LIKE concat('%',#{OrgCodePath},'%') "+
            " </when>" +
            ""+
            " <when test='AppCode!=null'>" +
            " AND Orgs.AppCode=#{AppCode}"+
            " </when>" +
            ""+
            " <when test='AppCode==null'>" +
            " AND Orgs.AppCode IS NULL"+
            " </when>" +
            ""+
            " AND Users.AccountIsDisabled  IS NULL OR  Users.AccountIsDisabled!=1"+
            "" +
            "</script>")
  //  List<Users> getsUsersByOrgCode(String AppCode,String RoleCode,boolean IsOneLevel,String OrgCodePath);
    List<Users> getsUsersByOrgCode(@Param("AppCode") String AppCode,@Param("OrgCode") String OrgCode,@Param("IsOneLevel") boolean IsOneLevel,@Param("OrgCodePath") String OrgCodePath);
    @Select("<script>"+
            "SELECT Users.* from  Users INNER JOIN RoleUsers ON Users.Id=RoleUsers.UserId"+
            " INNER JOIN Roles ON Roles.Id=RoleUsers.RoleId  "+
            " INNER JOIN Apps ON Roles.AppId=Apps.Id " +
            ""+
            " <when test='AppCode!=null'>" +
            " WHERE Apps.AppCode=#{AppCode}"+
            " </when>" +
            ""+
            " <when test='AppCode==null'>" +
            " WHERE Apps.AppCode is #{AppCode}"+
            " </when>" +
            ""+
            " AND  Roles.RoleCode=#{RoleCode}"+
            " AND Users.AccountIsDisabled  IS NULL OR  Users.AccountIsDisabled!=1"+
            ""+
            "</script>")
    List<Users> getsUsersByRoleCode(GetUsersInput input);
    @Update("update users set Password=#{NewPassword} where id=#{Id}")
    int ChangePassword(@Param("Id") String Id,@Param("NewPassword") String NewPassword);

    @Select("<script>" +
            "select * from Users where 1=1 " +
            "" +
            " <when test='keyword!=null'>" +
            " AND (UserCode like concat('%',#{keyword},'%') or UserName like concat('%',#{keyword},'%') or DingUserId like concat('%',#{keyword},'%') )" +
            " </when>" +
            "" +
            " order by UserCode" +
            "</script>")
    public List<Users> searchUsersList(SearchUsersInput input);

    @Update("update users set AccountIsDisabled=#{AccountIsDisabled} where id=#{Id}")
    int changeAccountStatusById(ChangeAccountStatusInput input);

    @Update("update users set AccountIsDisabled=#{AccountIsDisabled} where UserCode=#{UserCode}")
    int changeAccountStatusByUserCode(ChangeAccountStatusInput input);
}
